package br.com.rampage.controller.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.com.rampage.model.bean.Paciente;
import br.com.rampage.model.bean.UnidadeSaude;
import br.com.rampage.model.bo.PacienteBO;
import br.com.rampage.model.bo.UnidadeSaudeBO;
import br.com.rampage.utils.AppUtils;
import br.com.rampage.utils.ConnectionUsersUtil;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/Login")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String user = request.getParameter("user");
		String password = request.getParameter("password");
		String isPaciente = request.getParameter("paciente");

		if (isPaciente != null) { // paciente
			Paciente p = new PacienteBO().carregar(user);
			if (p != null && p.getSenha().equals(password)) {
				HttpSession session = request.getSession(true);
				ConnectionUsersUtil.setUserOfSessionId(session.getId(), p);
				AppUtils.getInstace().setDados(ConnectionUsersUtil.getUserFromSessionId(request.getSession().getId()));
				RequestDispatcher dispatcher = request
						.getRequestDispatcher("/carteira.jsp");
				dispatcher.forward(request, response);
			} else {
				erroLogin(request, response);
			}
		} else if (user.equals("admin")) {
			HttpSession session = request.getSession(true);
			ConnectionUsersUtil.setUserOfSessionId(session.getId(), null);
			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/system.jsp");
			AppUtils.getInstace().setDados(request.getSession().getId());
			dispatcher.forward(request, response);
		} else { // unidade de saude
			try {
				Integer id = Integer.parseInt(user);
				UnidadeSaude u = new UnidadeSaudeBO().carregar(id);
				if (u != null && u.getSenha().equals(password)) {
					HttpSession session = request.getSession(true);
					ConnectionUsersUtil.setUserOfSessionId(session.getId(), u);
					RequestDispatcher dispatcher = request
							.getRequestDispatcher("/system.jsp");
					AppUtils.getInstace().setDados(ConnectionUsersUtil.getUserFromSessionId(request.getSession().getId()));
					dispatcher.forward(request, response);
				} else {
					erroLogin(request, response);
				}
			} catch (Exception e) {
				erroLogin(request, response);
			}
		}
		
		

	}

	private void erroLogin(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		response.setContentType("text/html;charset-UTF-8");
		PrintWriter writer = response.getWriter();
		new Message().print(writer, TypeReturnServlet.danger,
				"Login/Senha incorreto!");

		RequestDispatcher dispatcher = request
				.getRequestDispatcher("/login.jsp");
		dispatcher.include(request, response);
	}

}
